package com.goudan.mybatisplus.controller;

import com.goudan.mybatisplus.entity.LgdUser;
import com.goudan.mybatisplus.service.LgdUserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 性能测试 前端控制器
 * </p>
 *
 * @author lgd
 * @since 2021-09-08 11:07:08
 */
@RestController
@RequestMapping("/lgd")
public class PerformanceController {

    @Resource
    private LgdUserService lgdUserService;

    @RequestMapping("saveBatchUser")
    public String saveBatchUser(Integer count) {
        long begin = System.currentTimeMillis();
        List<LgdUser> userList = createUsers(count);
        lgdUserService.saveBatch(userList, 50000);
        long end = System.currentTimeMillis();
        System.out.println("插入" + count + "条数据, 耗时" + (end - begin) + "ms");
        return "成功";
    }

    private static List<LgdUser> createUsers(int count) {
        List<LgdUser> userDemoList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            LgdUser user = new LgdUser();
            user.setAddress("66666");
            user.setUserName("金彪");
            user.setAge(20);
            user.setPhoneNo("13888888888");
            userDemoList.add(user);
        }
        return userDemoList;
    }

}
